<?php
declare (strict_types=1);

namespace app\admin\service\market\advert;

use app\admin\logic\market\advert\AdvertLink as AdvertLinkLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\facade\Request;
use think\Response;

/**
 * 广告位-内容管理
 */
class AdvertLink extends BaseService
{
    /**
     * 配置列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-26
     */
    public function index(int $advert_id)
    {
        $map['advert_id'] = $advert_id;
        $data             = app(AdvertLinkLogic::class)->getList($map);

        // 定义字段
        $keyList = [
            'link_type_text' => ListItem::make('custom')->title('广告类型'),
            'image'          => ListItem::make('custom')->title('广告图'),
            'link_to'        => ListItem::make('custom')->title('跳转至'),
            'update_time'    => ListItem::make()->title('最后更新时间'),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 新建
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-26
     */
    public function create(int $advert_id): Response
    {
        if (Request::isPost()) {
            $data              = Request::post();
            $data['advert_id'] = $advert_id;
            $res               = app(AdvertLinkLogic::class)->createLink($data);

            return static::parseToData($res->toArray(), 1, '添加成功');
        }

        $type = app(\app\admin\logic\market\advert\Advert::class)->getPlaceType($advert_id);

        $imageHelp = $type === 1 ? '864px*370px' : '690px*280px';

        $keyList = [
            'image' => FormItem::make('custom')->title('广告图片')->required()->help('建议尺寸：' . $imageHelp),
            'links' => FormItem::make('custom')->title('跳转设置')->min(0)->precision(2)->required(),
        ];

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey    = $keyList;
        $form->saveMethod = 'POST';

        $form->savePostUrl = '/admin/market/advert/' . $advert_id . '/manage/create';

        return $form->send();
    }

    /**
     * 编辑
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-26
     */
    public function edit(int $advert_id, int $link_id): Response
    {
        if (Request::isPut()) {
            $data = Request::put();
            app(AdvertLinkLogic::class)->editLink($link_id, $data);

            return static::parseToData([], 1, '编辑成功');
        }

        $data        = app(AdvertLinkLogic::class)->getLink($link_id)->hidden(['link_type', 'link_href', 'link_data_type', 'link_data_id', 'sort']);
        $data->links = [
            'link_type'      => $data->getAttr('link_type'),
            'link_data_type' => $data->getAttr('link_data_type'),
            'link_data_id'   => $data->getAttr('link_data_id'),
            'link_href'      => $data->getAttr('link_href'),
        ];

        $type = app(\app\admin\logic\market\advert\Advert::class)->getPlaceType($advert_id);

        $imageHelp = $type === 1 ? '864px*370px' : '690px*280px';

        $keyList = [
            'image' => FormItem::make('custom')->title('广告图片')->required()->help('建议尺寸：' . $imageHelp),
            'links' => FormItem::make('custom')->title('跳转设置')->min(0)->precision(2)->required(),
        ];

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey    = $keyList;
        $form->saveMethod = 'PUT';
        $form->datas      = $data;

        $form->savePostUrl = '/admin/market/advert/' . $advert_id . '/manage/' . $link_id . '/edit';

        return $form->send();
    }

    /**
     * 删除配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-14
     *
     * @param int $link_id
     * @return Response [type]                             [description]
     */
    public function delete(int $link_id): Response
    {
        app(AdvertLinkLogic::class)->deleteLink($link_id);

        return static::parseToData([], 1, '删除成功');
    }
}
